package Driver;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

import EventManager.Administrator;
import EventManager.ClientPerson;
import EventManager.CreateEventAdminRequest;
import EventManager.RequestProcessor;
import EventManager.ViewAllEventsRequest;
import Model.Person;
import Model.Venue;
import Model.VenueTimeslot;
import Service.InputManager;
import Service.OutputManager;

/**
 * Manual operation mode strategy
 * 
 * @author Yury Kitaev
 *
 */
public class ManualOperationStrategy implements OperationStrategy
{
	@Override
	public void run()
	{
		OutputManager.println("Manual Operation Mode Engaged.\n");
		
		String firstName = "";
		String lastName = "";
		OutputManager.print("Enter operator's name (First Last): ");
		Scanner kb = new Scanner(System.in);
		firstName = kb.next();
		lastName = kb.nextLine();
		
		OutputManager.offerChoiceMenu("What role does " + firstName  + " have in the system?", "Administrator", "Client");
		int role = InputManager.readInput(1, 2);
		
		if (1 == role)
		{
			Person person = new Administrator(firstName, lastName);
			OutputManager.println("Person created: " + person + "\n");
			playAdmin(person);
		}
		else
		{
			Person person = new ClientPerson(firstName, lastName);
			OutputManager.println("Person created: " + person + "\n");
			playClient(person);
		}
	}
	
	/**
	 * Manual admin role play
	 * @param person
	 */
	protected void playAdmin(Person person)
	{
		OutputManager.println("\nHello, " + person.getFirstName() + ", you are logged in as ADMIN!\n");
		
		int response = 0;
		while (response != 3)
		{
			OutputManager.offerChoiceMenu("What would you like to do next?", "View all available events", "Create an event", "Return to main menu");
			response = InputManager.readInput(1, 3);
			if (1 == response)
			{
				viewAllEvents();
			}
			else if (2 == response)
			{
				createEvent(person);
			}
		}
	}
	
	/**
	 * Manual event creation
	 */
	private void createEvent(Person user) 
	{
		Scanner kb = new Scanner (System.in);
		
		OutputManager.print("Event title: ");
		String title = kb.nextLine();

		
		boolean dateOk = false;
		Date date = null;
		while (!dateOk)
		{
			try
			{
				OutputManager.print("Date and time (yyyy-MM-dd HH:mm): ");
				SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
				date = dateFormat.parse(kb.nextLine());
				dateOk = true;
			}
			catch (ParseException e)
			{
				OutputManager.println("Wrong format, try again.");
			}
		}
		OutputManager.print("Duration (min): ");
		int duration = kb.nextInt();
		
		OutputManager.print("Capacity (persons): ");
		int capacity = kb.nextInt();
		
		OutputManager.print("Event venue: ");
		String venue = kb.next();
		
		
		Venue venuespec = new Venue(venue, "", capacity);
		VenueTimeslot venueTimeslot = new VenueTimeslot(venuespec);
		
		
		CreateEventAdminRequest request = null;
		Administrator admin = null;
		try
		{
			request = new CreateEventAdminRequest(title, date, duration, capacity, venueTimeslot, admin );
			admin = (Administrator) user;
			RequestProcessor.process(request, admin);
			RequestView viewer = new RequestView();
			viewer.view(request);
			OutputManager.print("\n\nEvent added.\nYou can now view the event list to see it.\n\n");
		}
		catch (Exception e)
		{
			OutputManager.println("Error: You must be an administrator to do this!");
			return;
		}
	}

	/**
	 * Manual client role play
	 * @param person
	 */
	protected void playClient (Person person)
	{
		OutputManager.println("\nHello, " + person.getFirstName() + ", welcome to ERS.\n");
		
		int response = 0;
		while (response != 2)
		{
			OutputManager.offerChoiceMenu("What would you like to do next?", "View all available events", "Return to main menu");
			response = InputManager.readInput(1, 2);
			if (1 == response)
			{
				viewAllEvents();
			}
		}
	}

	/**
	 * Requests for all events and displays them
	 */
	private void viewAllEvents() 
	{
		OutputManager.offerChoiceMenu("What kind of events would you like to see?", "All events in the catalog", "Only open events");
		int response = InputManager.readInput(1, 2);
		ViewAllEventsRequest r = null;
		if (1 == response)
		{
			r = new ViewAllEventsRequest(false);
		}
		else
		{
			r = new ViewAllEventsRequest(true);
		}
		r = (ViewAllEventsRequest) RequestProcessor.process(r);
		
		RequestView viewer = new ViewAllEventsRequestView();
		viewer.view(r);
	}
}
